package com.code.leetcode._202504;
/** 在排序数组中查找元素的第一个和最后一个位置 ，难度为 中等。**/
public class FirstAndLastLoca {

    public static void main(String[] args) {
        FirstAndLastLoca firstAndLastLoca = new FirstAndLastLoca();
        System.out.println(firstAndLastLoca.searchRange(new int[]{5,7,8,8,8,10},8).toString());
    }
    ///二分
    public int[] searchRange(int[] nums, int t) {
        int[] ans = new int[]{-1, -1};
        int n = nums.length;
        if (n == 0) return ans;
        int l = 0, r = n - 1;
        while (l < r) {
            int mid = l + r >> 1;//这个地方等学习以下/2 其实和>>1是一样的
            if (nums[mid] >= t) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        if (nums[r] != t) return ans;
        ans[0] = r;
        l = 0; r = n - 1;
        while (l < r) {
            int mid = l + r + 1 >> 1;
            if (nums[mid] <= t) l = mid;
            else r = mid - 1;
        }
        ans[1] = r;
        return ans;
    }


}
